var mongoose=require('mongoose');
var Post=mongoose.model('Post',{
    title:String,
    time:Date,
    count:Number,
    random:String
});
var db=mongoose.connect('mongodb://localhost/MongoDB_QuanWei_2');

var title='post13-2';
Post.update({title:title},{
    $setOnInsert:{
        time:new Date,
        count:0
    },
    random:Math.random()*100|0
},{upsert:true},(err,r)=>{
    if(err)throw err;
    console.log('Update:',r);

    Post.find({title:title},(e,r)=>{
        if(e)throw e;
        console.log('Find:\n',JSON.stringify(r,null,2));
        process.exit(1);
    });

});


/*
 * $setOnInsert:{field:value}
 *
 * 执行update并开启upsert时，此操作会保证只会有创建文档时为指定字段赋值，之后的所有更新操作中，这个字段的值不再会被更新
 *
 * */





